<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;

class StorageController extends AdminBaseController
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 文件存储
     * @adminMenu(
     *     'name'   => '文件存储',
     *     'parent' => 'admin/Setting/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文件存储',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $data = cmf_get_option('storage');
        $storage[0] = ['name' => '本地','type'=>0];
        $storage[1] = ['name' => '阿里云存储', 'type' => 0];
        if($data) {
            if (!empty($data['type'])) {
                $storage[1]['type']  = 1;
            } else {
                $storage[0]['type'] = 1;
            }
        }

        $this->assign('data',$data);
        $this->assign('storages',$storage);

       return $this->fetch();
    }

    /**
     * 文件存储
     * @adminMenu(
     *     'name'   => '文件存储设置提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文件存储设置提交',
     *     'param'  => ''
     * )
     */
    public function settingPost()
    {
        $storage = $this->request->post();
        if($storage['type'] ==1){
            $storage['KeyId'] = isset($storage['KeyId'])?trim($storage['KeyId']):'';
            $storage['KeySecret'] = isset($storage['KeySecret'])?trim($storage['KeySecret']):'';
            $storage['bucket'] = isset($storage['bucket'])?trim($storage['bucket']):'';
            $storage['endpoint'] = isset($storage['endpoint'])?trim($storage['endpoint']):'';
            if(empty($storage['KeyId'] )){ $this->error("AccessKey 不能为空！", ''); }
            if(empty($storage['KeySecret'] )){ $this->error("SecretKey 不能为空！", ''); }
            if(empty($storage['bucket'] )){ $this->error("存储空间名称 不能为空！", ''); }
            if(empty($storage['endpoint'] )){ $this->error("外网域名 不能为空！", ''); }

        }else{
            $storage= [];
            $storage['type'] = 0;
        }

        cmf_set_option('storage', $storage);
        $this->success("设置成功！", '');

    }


}